package algorithm.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

/**
 * @Author: Hevery
 * @Description:
 * @Date: Created on 2017/9/8
 * @Modified By:
 */
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        Node root = null;
        while(scan.hasNext()){
            String str = scan.nextLine();
            int index1 = str.indexOf(":");
            int index2 = str.indexOf("|");
            if(index1 == -1){
                root = new Node(Integer.parseInt(str));
            }else{
                int nodeValue = Integer.parseInt(str.substring(0,index1));
                int leftValue = Integer.parseInt(str.substring(index1+1,index2));
                int rightValue = Integer.parseInt(str.substring(index2+1));
                Node node = Main.findNode(root,nodeValue);
                Node left = Main.createNode(leftValue);
                Node right = Main.createNode(rightValue);
                node.left = left;
                node.right = right;
            }
        }
        System.out.println(isBST(root));
    }

    public static Node findNode(Node root ,int value){
        if(root !=null){
            if(root.value == value){
                return root;
            }
            findNode(root.left,value);
            findNode(root.right,value);
        }
            return null;
    }
    public static Node createNode(int value){
        Node node = null;
        if(value != -1){
            node = new Node(value);
        }
        return node;
    }
    public static int isBST(Node root){
        if(root == null){
            return 1;
        }
        int res = 1;
        Node pre = null;
        Node cur1 = root;
        Node cur2 = null;
        while(cur1 != null){
            cur2 = cur1.left;
            if(cur2 != null){
                while(cur2.right != null && cur2.right!= cur1){
                    cur2 = cur2.right;
                }
                if(cur2.right == null){
                    cur2.right = cur1;
                    cur1 = cur1.left;
                    continue;
                }else{
                    cur2.right = null;
                }
            }
            if(pre != null && pre.value > cur1.value){
                res = 0;
            }
            pre = cur1;
            cur1 = cur1.right;
        }
        return res;
    }
}
class Node{
    public int value ;
    public Node left;
    public Node right;
    public Node(int value){
        this.value = value;
    }
}